package com.base.myproject.client.HR.recruitment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com
 * 2010-11-3
 * 人力申请表
 * 
 */
public class RecruitmentRequest extends BaseJDBCPanel {
	public RecruitmentRequest() {
		init();
	}

	public void init() {
		initwidget();
		registerMainStore(store);
	}

	private void initwidget() {
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.add(getBaseGrid());
		vp.add(panel);
		FieldSet fs1 = new FieldSet();
		fs1.add(createForm());
		fs1.setHeading("人力申请表");
		vp.add(fs1);
		add(vp);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("requestNo");
		type.getField("requestNo").setType(String.class);
		type.addField("depCode");
		type.getField("depCode").setType(String.class);
		type.addField("requestDate");
		type.getField("requestDate").setType(Date.class);
		type.addField("position");
		type.getField("position").setType(String.class);
		type.addField("grade");
		type.getField("grade").setType(String.class);
		type.addField("explain");
		type.getField("explain").setType(String.class);
		type.addField("replaceEmpbyeeName");
		type.getField("replaceEmpbyeeName").setType(String.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		type.addField("age");
		type.getField("age").setType(Integer.class);
		type.addField("sex");
		type.getField("sex").setType(String.class);
		type.addField("education");
		type.getField("education").setType(String.class);
		type.addField("experience");
		type.getField("experience").setType(String.class);
		type.addField("skills");
		type.getField("skills").setType(String.class);
		type.addField("character");
		type.getField("character").setType(String.class);
		type.addField("hirePeople");
		type.getField("hirePeople").setType(String.class);
		type.addField("reportDate");
		type.getField("reportDate").setType(Date.class);
		type.addField("salary");
		type.getField("salary").setType(Double.class);
		type.addField("remark");
		type.getField("remark").setType(String.class);
		type.addField("isThrough");
		type.getField("isThrough").setType(Boolean.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_recruitmentRequest");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_recruitmentRequest where 1=2");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseFormPanel form = null;
	BaseComboBoxForm<BaseModelData> C_num3;

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore(), false);  //false设置让form不会提示用户新
																	//增加东西，由grid控制增加
		TableLayout tl = new TableLayout(8);
		form.setBodyBorder(false);
		form.setPadding(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		TableData td2 = new TableData();  //定义一个table用来装area
		td2.setRowspan(1);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		td2.setColspan(7);  //定义每个area占7列的位置

		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("部门编号:"), td);
		C_num3 = new BaseComboBoxForm<BaseModelData>("id", "name",
				"systeminfo",
				"where len(id)=(select max(len(id)) from systeminfo)", true);
		C_num3.setName("depCode");
		C_num3.setWidth(127);
		form.add(C_num3, td);
		form.add(new Label("申请日期:"), td);
		DateField C_data4 = new DateField();
		C_data4.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_data4.setWidth(127);
		C_data4.setName("requestDate");
		form.add(C_data4, td);
		form.add(new Label("申请职位:"), td);
		TextField<String> C_num5 = new TextField<String>();
		C_num5.setWidth(127);
		C_num5.setName("position");
		form.add(C_num5, td);
		form.add(new Label("职位级别:"), td);
		BaseComboBoxX C_num6 = new BaseComboBoxX("name", "dictionary",
				"where type='zy'");
		C_num6.setWidth(127);
		C_num6.setName("grade");
		form.add(C_num6, td);

		form.add(new Label("教育程度:"), td);
		BaseComboBoxX C_num12 = new BaseComboBoxX("name", "dictionary",
		" where type = 'hr_education'");
		C_num12.setName("education");
		C_num12.setWidth(127);
		form.add(C_num12, td);
		form.add(new Label("工作年限:"), td);
		BaseComboBoxX C_num13 = new BaseComboBoxX("name", "dictionary",
		" where type = 'experience'");
		C_num13.setWidth(127);
		C_num13.setName("experience");
		form.add(C_num13, td);
		form.add(new Label("技能要求:"), td);
		TextField<String> C_num14 = new TextField<String>();
		C_num14.setWidth(127);
		C_num14.setName("skills");
		form.add(C_num14, td);
		form.add(new Label("性格要求:"), td);
		TextField<String> C_num15 = new TextField<String>();
		C_num15.setWidth(127);
		C_num15.setName("character");
		form.add(C_num15, td);

		form.add(new Label("替代员工姓名:"), td);
		TextField<String> C_num8 = new TextField<String>();
		C_num8.setWidth(127);
		C_num8.setName("replaceEmpbyeeName");
		form.add(C_num8, td);
		form.add(new Label("年龄:"), td);
		NumberField C_num10 = new NumberField();
		C_num10.setWidth(127);
		C_num10.setPropertyEditorType(Integer.class);
		C_num10.setName("age");
		form.add(C_num10, td);
		form.add(new Label("性别:"), td);
		BaseComboBoxX C_num11 = new BaseComboBoxX("name", "dictionary",
		" where type = 'hr_sex'");
		C_num11.setWidth(127);
		C_num11.setName("sex");
		form.add(C_num11, td);
		form.add(new Label("离职原因:"), td);
		TextField<String> C_num20 = new TextField<String>();
		C_num20.setWidth(127);
		C_num20.setName("status");
		form.add(C_num20, td);

		form.add(new Label("新增职位说明:"), td);
		TextArea C_num7 = new TextArea();
		C_num7.setWidth(650);
		C_num7.setHeight(150);
		C_num7.setName("explain");
		form.add(C_num7, td2);
		form.inited();
		return form;
	}

	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}

		return grid;
	}

	void createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("depCode", "申请部门", 80);
		//渲染，不然会得到怪东西
		GridCellRenderer<ModelData> renderer1 = new GridCellRenderer<ModelData>() {
			public String render(ModelData model, String property,
					ColumnData d, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid) {
				if (model.get(property) != null
						&& model.get(property) instanceof ModelData) {
					return ((ModelData) model.get(property)).get("name"); //返回model里面的name属性
				}
				return model.get(property);
			}
		};
		C2.setRenderer(renderer1);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("requestDate", "申请日期", 80);
		C3.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("position", "职位名称", 80);
		columns.add(C4);

		ColumnConfig C6 = new ColumnConfig("grade", "职位级别", 80);
		GridCellRenderer<ModelData> renderer = new GridCellRenderer<ModelData>() {
			public String render(ModelData model, String property,
					ColumnData d, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid) {
				if (model.get(property) != null
						&& model.get(property) instanceof ModelData) {
					return ((ModelData) model.get(property)).get("value");
				}
				return model.get(property);
			}
		};

		C6.setRenderer(renderer);
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("education", "教育程度", 80);
		C7.setRenderer(renderer);
		columns.add(C7);

		ColumnConfig C8 = new ColumnConfig("replaceEmpbyeeName", "取代员工", 80);
		columns.add(C8);

		ColumnConfig C11 = new ColumnConfig("status", "退职位原因", 80);
		columns.add(C11);

		ColumnConfig C14 = new ColumnConfig("age", "年龄", 40);
		columns.add(C14);

		ColumnConfig C15 = new ColumnConfig("sex", "性别", 40);
		C15.setRenderer(renderer);
		columns.add(C15);

		ColumnConfig C16 = new ColumnConfig("experience", "工作年限", 80);
		C16.setRenderer(renderer);
		columns.add(C16);

		ColumnConfig C17 = new ColumnConfig("skills", "技能要求", 80);
		columns.add(C17);

		ColumnConfig C18 = new ColumnConfig("character", "个性要求", 80);
		columns.add(C18);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.setSize(840, 170);
	}
}
